package com.caiwangdai.app

import android.app.Activity
import android.app.Application
import android.content.Context
import android.os.Bundle
import android.support.multidex.MultiDex
import com.bumptech.glide.Glide
import com.caiwangdai.app.BuildConfig
import com.caiwangdai.app.interfaces.MyActivityLifeCycleCallbacks
import com.caiwangdai.app.ui.MainActivity
import com.orhanobut.logger.AndroidLogAdapter
import com.orhanobut.logger.Logger
import com.orhanobut.logger.PrettyFormatStrategy
import www.weimu.io.silentupdate.SilentUpdate
import java.util.*
import kotlin.properties.Delegates


class AppData : Application() {


    //伴随对象
    companion object {
        var context: AppData by Delegates.notNull()
        private val activityList = Stack<Activity>()

        fun getCurrentActivity() = activityList.peek()!!


        fun closeActivityExceptMain() {
            activityList.filter { it !is MainActivity }.forEach { it.finish() }
        }

        //退出app
        fun exitApp() {
            activityList.forEach { it.finish() }
        }
    }


    override fun attachBaseContext(base: Context?) {
        super.attachBaseContext(base)
        MultiDex.install(this)
    }

    override fun onCreate() {
        super.onCreate()
//        if (LeakCanary.isInAnalyzerProcess(this)) {
//            // This process is dedicated to LeakCanary for heap analysis.
//            // You should not init your app in this process.
//            return;
//        }
//        LeakCanary.install(this);
        context = this
        initLogger()
        initActivityCallback()
        SilentUpdate.init(this)
    }


    private fun initLogger() {
        val formatStrategy = PrettyFormatStrategy.newBuilder()
                .showThreadInfo(false)  // (Optional) Whether to show thread info or not. Default true
                .methodCount(1) // (Optional) How many method line to show. Default 2
                .tag("weimu")   // (Optional) Global tag for every log. Default PRETTY_LOGGER
                .build()

        Logger.addLogAdapter(object : AndroidLogAdapter(formatStrategy) {
            override fun isLoggable(priority: Int, tag: String?) = BuildConfig.DEBUG
        })
    }


    private fun initActivityCallback() {
        registerActivityLifecycleCallbacks(object : MyActivityLifeCycleCallbacks() {

            override fun onActivityCreated(activity: Activity?, savedInstanceState: Bundle?) {
                activityList.add(activity)
            }

            override fun onActivityDestroyed(activity: Activity?) {
                activityList.remove(activity)
            }
        })
    }

    //Glide的优化操作01
    override fun onTrimMemory(level: Int) {
        super.onTrimMemory(level)
        if (level == TRIM_MEMORY_UI_HIDDEN) {
            Glide.get(this).clearMemory()
        }
        Glide.get(this).trimMemory(level)
    }

    //Glide的优化操作02
    override fun onLowMemory() {
        super.onLowMemory()
        //内存低的时候清理Glide缓存
        Glide.get(this).clearMemory()
    }


}
